package gov.fema.hazus.bdc.client;

import gov.fema.hazus.bdc.client.login.Login;
import gov.fema.hazus.bdc.client.map.Map;
import gov.fema.hazus.bdc.client.settings.Settings;
import gov.fema.hazus.bdc.client.ui.BuildingEditor;
import gov.fema.hazus.bdc.client.ui.BuildingList;
import gov.fema.hazus.bdc.client.ui.LeftNavigation;
import gov.fema.hazus.bdc.client.ui.RightNavigation;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class BuildingDataCollector implements EntryPoint {

    private static BuildingDataCollector singleton;
    public static BuildingDataCollector get()
    {
        if( singleton == null )
        {
            singleton = new BuildingDataCollector();
        }
        return singleton;
    }

    private VerticalPanel vpanel = new VerticalPanel();
    
    private int width;
	private int height;
	
	private Login login = Login.get();
	private MainUI main = MainUI.get();
	
	private LeftNavigation leftNavigation = LeftNavigation.get();
	private RightNavigation rightNavigation = RightNavigation.get();
	private BuildingEditor buildingEditor = BuildingEditor.get();
	private Settings settings = Settings.get();
    
    public void onModuleLoad() {
    	
    	Window.enableScrolling(true);
    	
    	// set up vpanel
    	vpanel.add(login);
    	vpanel.setCellHorizontalAlignment(login, HasHorizontalAlignment.ALIGN_CENTER);
    	vpanel.setStylePrimaryName("incast-container");
    	
    	GWT.log("MainUI.get " + MainUI.get().hashCode(), null);
    	GWT.log("Login.get " + Login.get().hashCode(), null);
    	
    	vpanel.add(main);
    	
    	// let's hide everything...
    	
    	
    	// load vpanel into root panel
    	//RootPanel.get().add(vpanel);
    	/*
    	 * RootPanel + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    	 * Chrome Win: NOPE -- all widgets displayed instead of selectively hidden
    	 * FF Win: YEP -- works right
    	 * IE7 Win: YEP -- works right
    	 */
    	/*
    	 * RootPanel + <!DOCTYPE HTML>
    	 * Chrome Win: NOPE -- all widgets displayed instead of selectively hidden
    	 * FF Win: NOPE - height for building list and map doesn't work
    	 * IE7 Win: YEP, just fine
    	 */
    	
    	RootLayoutPanel.get().add(vpanel);
    	/*
    	 * RootLayoutPanel + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    	 * Chrome Win: YEP, right height for map and building list
    	 * FF Win: YEP, right height for map and building list
    	 * IE7 Win: NOPE, blank screen
    	 * IE7 Win + Google Chrome Frame: YEP, just fine
    	 */
    	/*
    	 * RootLayoutPanel + <!DOCTYPE HTML>
    	 * Chrome Win: NOPE - height for building list and map doesn't work
    	 * FF Win: NOPE - height for building list and map doesn't work
    	 * IE7 Win: YEP -- works right
    	 */
    	
    	showLoginOrMainUI();
    	
    }
    
    private void showLoginOrMainUI()
    {
    	
    	if( Login.get().isValid() )
    	{
    		login();
    	}
    	else
    	{
    		showLoginPage();
    	}

    }
    
	public void showLoginPage() {
		
		/* 
		 * It seems that the new GWT does not like
		 * just the set visible.
		 * 
		 * Changed doctype to just HTML and the map disappeared
		 * as expected, but not the other UIs.
		 * 
		 * Now changing to RootLayoutPanel instead of RootPanel
		 * 
		 */
		GWT.log("MainUI.get " + MainUI.get().hashCode(), null);
    	GWT.log("Login.get " + Login.get().hashCode(), null);
    	
		Map.get().setVisible(false);
		MainUI.get().setVisible(false);
		Login.get().setVisible(true);
	}
	
	
	public void login()
	{
		GWT.log("MainUI.get " + MainUI.get().hashCode(), null);
    	GWT.log("Login.get " + Login.get().hashCode(), null);
    	
    	MainUI.get().setVisible(true);
		Login.get().setVisible(false);
		MainUI.get().actionWrapperLogin();
	}
	
	
}
